package com.example.computer_exam_training_sys.service.impl;

import com.example.computer_exam_training_sys.entity.UserFavorite;
import com.example.computer_exam_training_sys.mapper.UserFavoriteMapper;
import com.example.computer_exam_training_sys.service.UserFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserFavoriteServiceImpl implements UserFavoriteService {

    @Autowired
    private UserFavoriteMapper mapper;

    @Override
    public void addFavorite(UserFavorite favorite) {
        favorite.setCreateTime(LocalDateTime.now());
        mapper.insert(favorite); // 如果重复插入，这里抛 DuplicateKeyException
    }

    @Override
    public void removeFavorite(int userId, int questionId) {
        mapper.delete(userId, questionId);
    }

    @Override
    public boolean isFavorite(int userId, int questionId) {
        return mapper.isFavorited(userId, questionId) > 0;
    }
}
